// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
  // env:'wccloud-fm7zt',
  traceUser:true
});

const db = cloud.database();
const command = db.command;
const wxContext = cloud.getWXContext();
// 云函数入口函数
exports.main = async (event, context) => {
  var docid = event.docid;
  var pic = event.pic;
  var news_title = event.news_title;
  var news_desc = event.news_desc;
  var oldtitle = event.oldtitle;
  var olddesc = event.olddesc;

  const results =  await db.collection('news').doc(docid).update({
    data: {
      // title: command.pull(oldtitle),
      // desc:command.pull(olddesc),
      title: command.set(news_title),
      desc:command.set(news_desc),
      pics:command.push(pic),
    }
  })

  return {
    results,
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}